package com.yeung.exam;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class 搜狐_迸发编程 {


    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock(); //定义一个互斥锁类型
        Condition word_worker = lock.newCondition();
        Condition number_worker = lock.newCondition();
        new Thread(new Runnable() {

            public void run() {

                try {
                    lock.lock();//获取锁
                    for (int i = 1; i <= 52; i++) {
                        System.out.print(i);
                        if (i % 2 == 0) {
                            word_worker.signalAll();//唤醒字母进程
                            try {
                                number_worker.await();//让数字进程等待
                            } catch (InterruptedException e) {
                            }
                        }
                    }
                } finally {
                    lock.unlock();//释放锁
                }
            }

        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    lock.lock();
                    for (int i = 0; i < 26; i++) {
                        System.out.print((char) ('A' + i) + " ");
                        number_worker.signalAll();
                        try {
                            if (i != 25) {//最后一个字母不用进入等待状态
                                word_worker.await();
                            }

                        } catch (InterruptedException e) {
                        }

                    }
                } finally {
                    lock.unlock();
                }

            }
        }).start();

    }
}
